﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace TestSystem.DAL.TestSystem
{
    public class UserAnswerDataAccess : DataAccess
    {
        public static IDataReader GetUserAnswersByTestID(int testID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "UserAnswers_GetByTestID", paramsArray);
        }

        public static IDataReader GetUserAnswersByQuizID(int quizID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@QuizID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, quizID)
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "UserAnswers_GetByQuizID", paramsArray);
        }

        public static IDataReader GetByID(int ID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@ID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, ID)
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "UserAnswers_GetByID", paramsArray);
        }

        public static IDataReader GetAll()
        {
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "UserAnswers_GetAll");
        }

        public static void Delete(int ID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@ID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, ID)     
                };
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "UserAnswers_Delete", paramsArray);
        }

        public static void Update(int userAnswerID, string userID, int quizID, int testID, int ID, string userAnswerBody)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@UserAnswerID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userAnswerID),
                    new SqlParameter("@UserID", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userID),
                    new SqlParameter("@QuizID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, quizID),
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID),
                    new SqlParameter("@ID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, ID),
                    new SqlParameter("@UserAnswerBody", SqlDbType.NText, NTEXT, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userAnswerBody)
                };
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "UserAnswers_Update", paramsArray);
        }

        public static int Insert(int userAnswerID, string userID, int quizID, int testID, string userAnswerBody)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@UserAnswerID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userAnswerID),
                    new SqlParameter("@UserID", SqlDbType.VarChar, 50, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userID),
                    new SqlParameter("@QuizID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, quizID),
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID),
                    new SqlParameter("@UserAnswerBody", SqlDbType.NText, NTEXT, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, userAnswerBody)
                };
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "UserAnswers_Insert", paramsArray));
        }


    }
}
